package mn.idax.exchange.otc.aspect;

import mn.idax.exchange.otc.annotation.SignAuth;
import mn.idax.exchange.otc.util.ParamUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.TreeMap;

/**
 * @Auther: dison
 * @Date: 2018/7/26 18:10
 * @Description:
 */
@Aspect
@Configuration
public class SignAuthAspect {

    // 定义切点Pointcut
    @Pointcut("@annotation(mn.idax.exchange.otc.annotation.SignAuth)")
    public void execute() {
    }

    @Around("execute() && @annotation(signAuth)")
    public Object doAround(ProceedingJoinPoint pjp, SignAuth signAuth) throws Throwable {
        String class_name = pjp.getTarget().getClass().getName();
        String method_name = pjp.getSignature().getName();
        Object[] args = pjp.getArgs();
        Object result = pjp.proceed();

        if (signAuth.ignore()){

            RequestAttributes ra = RequestContextHolder.getRequestAttributes();
            ServletRequestAttributes sra = (ServletRequestAttributes) ra;
            HttpServletRequest request = sra.getRequest();

            TreeMap treeMap = ParamUtils.treeMapToServletRequest(request);
            treeMap.forEach((k,v) ->{
                System.out.printf( k + "," + v);
            });
        }

        return result;
    }
}
